package com.hacpai.android.md.model.entity;

import android.support.annotation.NonNull;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.hacpai.android.md.model.util.EntityUtils;
import com.hacpai.android.md.util.FormatUtils;
import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.List;
import retrofit2.Response;

public class Result{

    /**
     * Indicates success.
     * SUCC = 0
     *
     *  Indicates not found.
     * NOT_FOUND = 1
     *
     * Indicates an error occurred.
     * ERR = -1
     */
    public static final Integer SUCC = 0;
    public static final Integer NOT_FOUND = 1;
    public static final Integer ERR = -1;

    protected Integer sc;
    protected String msg;

    public static class ArticleData{
        private List<Article> articles;
        private Pagination pagination;

        public List<Article> getArticles() {
            return articles;
        }

        public void setArticles(List<Article> articles) {
            this.articles = articles;
        }

        public Pagination getPagination() {
            return pagination;
        }

        public void setPagination(Pagination pagination) {
            this.pagination = pagination;
        }
    }

    public static class CommentData{
        private List<Comment> comments;
        private Pagination pagination;

        public List<Comment> getComments() {
            return comments;
        }

        public void setComments(List<Comment> comments) {
            this.comments = comments;
        }

        public Pagination getPagination() {
            return pagination;
        }

        public void setPagination(Pagination pagination) {
            this.pagination = pagination;
        }
    }

    public static class DomainData{
        private List<Domain> domains;
        private Pagination pagination;

        public List<Domain> getDomains() {
            return domains;
        }

        public void setDomains(List<Domain> domains) {
            this.domains = domains;
        }

        public Pagination getPagination() {
            return pagination;
        }

        public void setPagination(Pagination pagination) {
            this.pagination = pagination;
        }
    }


    public static class Data<T> extends Result {

        private T data;

        public T getData() {
            return data;
        }

    }

    public static class Error extends Result {

        @SerializedName("error_msg")
        private String errorMessage;

        public String getErrorMessage() {
            return errorMessage;
        }

    }

    public static <T extends Result> Error buildError(@NonNull Response<T> response) {
        try {
            return EntityUtils.gson.fromJson(response.errorBody().string(), Error.class);
        } catch (IOException | JsonSyntaxException e) {
            Error error = new Error();
            error.sc = ERR;
            switch (response.code()) {
                case 400:
                    error.errorMessage = "请求参数有误";
                    break;
                case 403:
                    error.errorMessage = "请求被拒绝";
                    break;
                case 404:
                    error.errorMessage = "资源未找到";
                    break;
                case 405:
                    error.errorMessage = "请求方式不被允许";
                    break;
                case 408:
                    error.errorMessage = "请求超时";
                    break;
                case 422:
                    error.errorMessage = "请求语义错误";
                    break;
                case 500:
                    error.errorMessage = "服务器逻辑错误";
                    break;
                case 502:
                    error.errorMessage = "服务器网关错误";
                    break;
                case 504:
                    error.errorMessage = "服务器网关超时";
                    break;
                default:
                    error.errorMessage = response.message();
                    break;
            }
            return error;
        }
    }

    public static Error buildError(@NonNull Throwable t) {
        Error error = new Error();
        error.sc = ERR;
        if (t instanceof UnknownHostException || t instanceof ConnectException) {
            error.errorMessage = "网络无法连接";
        } else if (t instanceof SocketTimeoutException) {
            error.errorMessage = "网络访问超时";
        } else if (t instanceof JsonSyntaxException) {
            error.errorMessage = "响应数据格式错误";
        } else {
            error.errorMessage = "未知错误：" + t.getLocalizedMessage();
        }
        return error;
    }


    //hac logindate

    public static class LoginDate extends Result {

        @SerializedName("userName")
        private String userName;

        @SerializedName("token")
        private String token;

        @SerializedName("needCaptcha")
        private String needCaptcha;

        public String getToken() {
            return token;
        }

        public String getNeedCaptcha() {
            return needCaptcha;
        }

        public String getUserName(){
            return userName;
        }

    }


    public static class UserDate extends Result {


        @SerializedName("user")
        private User user;

        public User getUser() {
            return user;
        }


    }

    public static class Login extends Result {

        private String id;

        @SerializedName("loginname")
        private String loginName;

        @SerializedName("avatar_url")
        private String avatarUrl;

        public String getId() {
            return id;
        }

        public String getLoginName() {
            return loginName;
        }

        public String getAvatarUrl() { // 修复头像地址的历史遗留问题
            return FormatUtils.getCompatAvatarUrl(avatarUrl);
        }

    }



    public static class CreateTopic extends Result {

        @SerializedName("topic_id")
        private String topicId;

        public String getTopicId() {
            return topicId;
        }

    }

    public static class ReplyTopic extends Result {

        @SerializedName("reply_id")
        private String replyId;

        public String getReplyId() {
            return replyId;
        }

    }


    public Integer getSc() {
        return sc;
    }

    public void setSc(Integer sc) {
        this.sc = sc;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


}
